package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.ExExamPapper;
import com.macro.mall.model.ExExamResultView;
import com.macro.mall.service.ExExamPapperService;
import com.macro.mall.utils.FilePortUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 考试管理Controller
 */
@Slf4j
@Controller
@Api(tags = "ExExamPapperController", description = "考试管理")
@RequestMapping("/exam")
public class ExExamPapperController {

    @Autowired
    private ExExamPapperService examPapperService;

    @ApiOperation(value = "获取单个考试信息")
    @RequestMapping(value = "/getByExamId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ExExamPapper> getByExamId(@RequestParam(value = "examId", defaultValue = "") Long examId
    ) {
        try {
            ExExamPapper examPapper = examPapperService.getById(examId);
            return CommonResult.success(examPapper);
        }catch (Exception e){
            log.error(e.getMessage());
            return CommonResult.failed("获取考题信息失败："+e.getMessage());
        }
    }

    @ApiOperation(value = "保存考试信息规格")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> save(@RequestBody ExExamPapper examPapper, BindingResult result) {
        CommonResult commonResult;
        try{
            examPapperService.save(examPapper);
            commonResult = CommonResult.success("保存成功！");
        }catch (Exception e){
            log.error("考试信息保存失败："+e.getMessage());
            commonResult = CommonResult.failed("考试信息保存失败："+e.getMessage());
        }
        return commonResult;
    }

    @ApiOperation(value = "获取考试统计信息")
    @RequestMapping(value = "/getExamResultList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult  getExamResultList(@RequestParam(value = "training_name", defaultValue = "") String training_name,
                                           @RequestParam(value = "community_name", defaultValue = "") String community_name,
                                           @RequestParam(value = "phone", defaultValue = "") String phone,
                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        if(!StringUtils.isBlank(training_name)){
            paramMap.put("training_name",training_name);
        }
        if(!StringUtils.isBlank(community_name)){
            paramMap.put("community_name",community_name);
        }
        if(!StringUtils.isBlank(phone)){
            paramMap.put("phone",phone);
        }
        try {
            List<Map<String,Object>>  resultList = examPapperService.selectExamResultList(paramMap,pageSize,pageNum);
            return CommonResult.success(CommonPage.restPage(resultList));
        }catch (Exception e){
            log.error(e.getMessage());
            return CommonResult.failed("获获取考试统计信息失败："+e.getMessage());
        }
    }
    @ApiOperation(value = "考试结果导出")
    @RequestMapping(value = "/exportExamResultList", method = RequestMethod.GET)
    @ResponseBody
    public void  exportExamResultList(@RequestParam(value = "training_name", defaultValue = "") String training_name,
                                      @RequestParam(value = "community_name", defaultValue = "") String community_name,
                                      @RequestParam(value = "phone", defaultValue = "") String phone,
                                      @RequestParam(value = "pageSize", defaultValue = "10000") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      HttpServletResponse response
    ) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (!StringUtils.isBlank(training_name)) {
            paramMap.put("training_name", training_name);
        }
        if (!StringUtils.isBlank(community_name)) {
            paramMap.put("community_name", community_name);
        }
        if (!StringUtils.isBlank(phone)) {
            paramMap.put("phone", phone);
        }
        try {
            //导出的表名
            String title = "考试结果";
            //表中第一行表头字段
            String[] headers = {"手机号", "姓名", "社区", "学院课程", "是否通过","分数","考试时间"};
            //实际数据结果集
            List<ExExamResultView> resultList = examPapperService.exportExamResultList(paramMap, pageSize, pageNum);
            //具体需要写入excel需要哪些字段，这些字段取自UserReward类，也就是上面的实际数据结果集的泛型
            List<String> listColumn = Arrays.asList("phone", "nickName", "communityName", "trainingName", "isPassName", "score", "createTime");
            try {
                FilePortUtil.exportExcel(response, title, headers, resultList, listColumn);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
